//
//  MarketSearchResultVC.m
//  FKGW
//
//  Created by song ce on 2017/5/18.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "MarketSearchResultVC.h"
#import "NearShopCell.h"
#import "ShopDetailViewController.h"
#define PI 3.1415926
@interface MarketSearchResultVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MarketSearchResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.vcName = @"商场搜索列表";
    self.dataSource = [NSMutableArray array];
    [self tableViewAndHeaderView];
    [self loadSearchData];
}

-(void)showTips{
      [MBProgressHUD showTipMessageInWindow:@"没有您的位置信息\r\n无法为您查询相关数据\r\n请到设置中修改定位权限"];
}
//2.11商场查询接口（商场列表）
-(void)loadSearchData{
    if(!LONGITUDE.length){
        [self.view addSubview:self.noDataIUmageV];
        [self performSelector:@selector(showTips) withObject:nil afterDelay:1];
        return;
    }
    [self.noDataIUmageV removeFromSuperview];
    NSDictionary * paraDic= @{@"marketName":self.marketName};
    __weak typeof(self)  weakSelf = self;
   [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:SELECTBYMARKETNAME paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"商场搜索结果----%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *listArr = resultDic[@"data"];
            if([listArr isKindOfClass:[NSNull class]]) {
                [MBProgressHUD dismissHUD];
                return ;
            }
            for(NSDictionary *dic in listArr){
                NearShopModel *model = [[NearShopModel alloc]init];
                model.name = dic[@"marketName"];
                model.id = dic[@"marketId"];
                model.address = dic[@"marketAddress"];
                model.storePic= dic[@"marketPic"];
                //计算距离
                model.distance = [self distanceByLon:dic[@"LON"] lat:dic[@"LAT"]];
                [weakSelf.dataSource addObject:model];
            }
            if(!weakSelf.dataSource.count){
                [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];
                return;
            }else{
                [weakSelf.noDataIUmageV removeFromSuperview];
            }
            [weakSelf.tableV reloadData];
            weakSelf.tableV.hidden = NO;
            [MBProgressHUD dismissHUD];
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:UITableViewStylePlain];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.hidden = YES;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"acqtell";
    NearShopCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"NearShopCell" owner:self options:nil]lastObject];
    }
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NearShopModel *model = self.dataSource[indexPath.row];
    ShopDetailViewController  *shopDetail = [[ShopDetailViewController alloc]init];
    shopDetail.vcName = model.name;
    shopDetail.marketId = model.id;
    shopDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:shopDetail animated:YES];
}

-(NSString*) distanceByLon:(NSString*)lon lat:(NSString*)lat {
    double  lat1  = [lat doubleValue];
    double  lon1 = [lon doubleValue];
    double lon2 = [[AppDelegate appDealegate].longitude doubleValue];
    double lat2 = [[AppDelegate appDealegate].latitude doubleValue];
    double er = 6378137; // 6378700.0f;
    double radlat1 = PI*lat1/180.0f;
    double radlat2 = PI*lat2/180.0f;
    double radlong1 = PI*lon1/180.0f;
    double radlong2 = PI*lon2/180.0f;
    if( radlat1 < 0 ) radlat1 = PI/2 + fabs(radlat1);// south
    if( radlat1 > 0 ) radlat1 = PI/2 - fabs(radlat1);// north
    if( radlong1 < 0 ) radlong1 = PI*2 - fabs(radlong1);//west
    if( radlat2 < 0 ) radlat2 = PI/2 + fabs(radlat2);// south
    if( radlat2 > 0 ) radlat2 = PI/2 - fabs(radlat2);// north
    if( radlong2 < 0 ) radlong2 = PI*2 - fabs(radlong2);// west
    double x1 = er * cos(radlong1) * sin(radlat1);
    double y1 = er * sin(radlong1) * sin(radlat1);
    double z1 = er * cos(radlat1);
    double x2 = er * cos(radlong2) * sin(radlat2);
    double y2 = er * sin(radlong2) * sin(radlat2);
    double z2 = er * cos(radlat2);
    double d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
    double theta = acos((er*er+er*er-d*d)/(2*er*er));
    double dist  = theta*er;
    return [NSString stringWithFormat:@"%ld",(NSInteger)dist];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
